package com.ztgf.sys.api.cms;


import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.sys.models.bo.SysMenuBO;
import com.ztgf.sys.models.bo.SysMenuSaveBO;
import com.ztgf.sys.models.bo.SysRoleMenuRelationBO;
import com.ztgf.sys.models.constant.SysConstant;
import com.ztgf.sys.models.dto.SysMenuDTO;
import com.ztgf.sys.models.dto.SysRoleMenuRelationDTO;
import com.ztgf.sys.models.service.ISysMenuService;
import com.ztgf.sys.models.service.ISysRoleMenuRelationService;
import com.ztgf.sys.models.service.ISysUserService;
import com.ztgf.sys.models.vo.SysMenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 菜单  前端控制器
 * </p>
 *
 * @author YUE123
 * @since 2020-05-26
 */
@RestController
@RequestMapping("/cms/sysMenu")
@Slf4j
@Api(value = "SysMenuController",tags = "菜单表接口")
public class SysMenuController {

    @DubboReference
    private ISysMenuService iSysMenuService;

    @DubboReference
    private ISysUserService sysUserService;

    @DubboReference
    private ISysRoleMenuRelationService sysRoleMenuRelationService;

    @PostMapping("/getMyMenuTreeListByRoleId")
    @ApiOperation(value = "根据角色Id获取属于自己的菜单",notes = "根据角色Id获取属于自己的菜单")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<List<SysMenuVO>> getMyMenuTreeListByRoleId(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        List<SysMenuVO> sysMenuVOList = iSysMenuService.selectMenuIdListMeByRoleId(id);
        return new Result<>(sysMenuVOList);
    }

    @PostMapping("/getMyMenuTreeListByUserId")
    @ApiOperation(value = "根据用户Id获取属于自己的菜单",notes = "根据用户Id获取属于自己的菜单")
    public Result<List<SysMenuVO>> getMyMenuTreeListByUserId(Long userId){
        if(userId == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        List<SysMenuVO> sysMenuVOList = sysUserService.getUserAllMenuTreeList(userId);
        return new Result<>(sysMenuVOList);
    }

    /**
     * 查询所有的菜单并集成菜单树
     * @return
     */
    @PostMapping("/getMenuTreeList")
    @ApiOperation(value = "获取所有的菜单(树形结构)",notes = "获取所有的菜单(树形结构)")
    public Result<List<SysMenuVO>> getMenuTreeList(){
        List<SysMenuVO> menuTreeList = sysUserService.getMenuTreeList();
        return new Result<>(menuTreeList);
    }

//    @PostMapping("/getNotMeMenuTreeList")
//    @ApiOperation(value = "根据角色Id获取不属于自己的菜单",notes = "根据角色Id获取不属于自己的菜单")
//    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
//    public Result<List<SysMenuVO>> getNotMeMenuTreeList(Long id){
//        if(id == null){
//            return new Result<>(CodeEnum.FAIL_100002 );
//        }
//        List<SysMenuVO> sysMenuVOList = iSysMenuService.selectMenuIdListNotMeByRoleId(id);
//        return new Result<>(sysMenuVOList);
//    }

    @ApiOperation(value = "添加菜单表",notes = "添加菜单表")
    @PostMapping(value = "/insert")
    public Result insert(SysMenuSaveBO sysMenuSaveBO) {
        boolean flag = iSysMenuService.add(sysMenuSaveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "修改菜单表",notes = "修改菜单表")
    @PostMapping("/update")
    public Result update(SysRoleMenuRelationBO bo){
        if(null == bo.getMenuId()){
            return new Result(CodeEnum.FAIL_100002 );
        }
        bo.setRoleId(SysConstant.ADMIN_ID);
        SysRoleMenuRelationDTO sysRoleMenuRelationDTO = sysRoleMenuRelationService.getByMenuAndRole(bo.getMenuId(), bo.getRoleId());
        sysRoleMenuRelationService.updateMenuRelation(bo,sysRoleMenuRelationDTO);
        return new Result<>();
    }

    @ApiOperation(value = "删除菜单表",notes = "删除菜单表")
    @ApiImplicitParam(name = "id",value = "区域表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        if(null == id){
            return new Result(CodeEnum.FAIL_100002 );
        }
        iSysMenuService.deleteByMenuId(id);
        return new Result<>();
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除菜单表",notes = "批量删除菜单表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        iSysMenuService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();

    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysMenuDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        SysMenuDTO sysMenuDTO = iSysMenuService.selectById(id);
        return new Result<>(sysMenuDTO);
    }

    @ApiOperation(value = "菜单列表",notes = "菜单列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysMenuDTO>> queryPageList(SysMenuBO sysMenuBO){
        Page<SysMenuDTO> page = new Page<>(sysMenuBO.getPageNo(), sysMenuBO.getPageSize());
        page = iSysMenuService.selectPage(page);
        return new Result<>(page);
    }
}

